package com.idlecode.component;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

public class MultipleSelectionCombo extends Composite {
	Text displayText = null;
	String[] comboItems = null;
	Set<Integer> comboSelection = new HashSet<>();
	Shell floatShell = null;
	Composite itemComposite = null;
	Button dropButton = null;

	public MultipleSelectionCombo(Composite parent, String[] items, int style) {
		super(parent, style);
		comboItems = items;
		init();
		displayText();
	}

	private void init() {
		GridLayout layout = new GridLayout(2, false); // 修改为2列
		layout.marginBottom = 0;
		layout.marginTop = 0;
		layout.marginLeft = 0;
		layout.marginRight = 0;
		layout.marginWidth = 0;
		layout.marginHeight = 0;
		setLayout(layout);

		displayText = new Text(this, SWT.BORDER);
		displayText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		// 添加显示下拉框的按钮
		dropButton = new Button(this, SWT.ARROW | SWT.DOWN);
		dropButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		dropButton.addMouseListener(new MouseAdapter() {
			public void mouseDown(MouseEvent event) {
				super.mouseDown(event);
				initFloatShell();
			}
		});

		// 鼠标点击文本框也可以显示下拉框
		displayText.addMouseListener(new MouseAdapter() {
			public void mouseDown(MouseEvent event) {
				super.mouseDown(event);
				initFloatShell();
			}
		});
	}

	private void initFloatShell() {
		Point p = displayText.getParent().toDisplay(displayText.getLocation());
		Point size = displayText.getSize();
		Rectangle shellRect = new Rectangle(p.x, p.y + size.y, size.x, 0);
		floatShell = new Shell(MultipleSelectionCombo.this.getShell(), SWT.NO_TRIM);

		GridLayout gl = new GridLayout(1, false);
		gl.marginBottom = 2;
		gl.marginTop = 2;
		gl.marginRight = 0;
		gl.marginLeft = 0;
		gl.marginWidth = 0;
		gl.marginHeight = 0;
		floatShell.setLayout(gl);

		itemComposite = new Composite(floatShell, SWT.NONE);
		GridLayout itemLayout = new GridLayout(1, false);
		itemComposite.setLayout(itemLayout);
		GridData gd = new GridData(GridData.FILL_BOTH);
		itemComposite.setLayoutData(gd);

		Color blue = Display.getCurrent().getSystemColor(SWT.COLOR_LIST_SELECTION);
		Color white = Display.getCurrent().getSystemColor(SWT.COLOR_WHITE);

		for (int i = 0; i < comboItems.length; i++) {
			Composite item = new Composite(itemComposite, SWT.NONE);
			item.setLayout(new GridLayout(1, false));
			item.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

			Button checkBox = new Button(item, SWT.CHECK);
			checkBox.setText(comboItems[i]);
			checkBox.setSelection(comboSelection.contains(i));
			if (comboSelection.contains(i)) {
				checkBox.setBackground(blue);
			}
			int index = i;
			checkBox.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					if (checkBox.getSelection()) {
						comboSelection.add(index);
						checkBox.setBackground(blue);
					} else {
						comboSelection.remove(index);
						checkBox.setBackground(white);
					}
					displayText();
				}
			});
		}

		floatShell.setSize(shellRect.width, 200);
		floatShell.setLocation(shellRect.x, shellRect.y);

		floatShell.addShellListener(new ShellAdapter() {
			public void shellDeactivated(ShellEvent arg0) {
				if (floatShell != null && !floatShell.isDisposed()) {
					displayText();
					floatShell.dispose();
				}
			}
		});

		floatShell.open();
	}

	private void displayText() {
		if (comboSelection != null && comboSelection.size() > 0) {
			if (comboItems.length == comboSelection.size()) {
				displayText.setText("全选");
			} else {
				displayText.setText("已选择" + comboSelection.size() + "项");
			}
		} else {
			displayText.setText("已选择0项");
		}
	}

	public void setText(String text) {
		displayText.setText(text);
	}

	public String getText() {
		return displayText.getText();
	}

	public Text getTextWidget() {
		return displayText;
	}

	public java.util.List<String> getSelectedItems() {
		java.util.List<String> selectedItems = new ArrayList<>();
		for (int index : comboSelection) {
			selectedItems.add(comboItems[index]);
		}
		return selectedItems;
	}

	public void setSelectedItems(String[] items) {
		comboSelection.clear();
		for (String item : items) {
			for (int i = 0; i < comboItems.length; i++) {
				if (comboItems[i].equals(item)) {
					comboSelection.add(i);
					break;
				}
			}
		}
		displayText();
	}
}
